Medium (4)
- Two Sum
- 20 March, 2026: 00.04.18 ❌
- Return wrong index
- BruteForce:
- Better:
- Optimal:
unordered_map: easy to accessmap: sorted useage
- 20 March, 2026: 00.04.18 ❌
- Sort Color
left→ tracks position for0mid→ current elementright→ tracks position for20→ swap withleft, move both1→ just movemid2→ swap withright, moverightonly
- Majority Element
- 20 March, 2026: 00.05.43 ✔
- Boyer–Moore process only gives candidates, not confirmed answers. You must count again to verify and compare with thresold
- 20 March, 2026: 00.05.43 ✔
- Kadane's Algorithm
- 20 March, 2026: 00.05.54 ✔
- Implment bruteforce and better approach
- place the condition in appropriate order
- 20 March, 2026: 00.05.54 ✔
- Print Sub Array
- In Kadane’s algorithm, you reset when the cumulative sum becomes negative, not when the current element is negative.
- Handle tie-breaking rules
- Prefer longer length
- If still tied → earlier starting index
- Stock Buy And Sell
- Rearrange Array Elements by Sign
- 07 April, 2026: 00.12.03 ❌
- Failed at optimization
- Instead of two container, use single container
- Failed at optimization
- 07 April, 2026: 00.12.03 ❌
- Next Permuation
- 07 April, 2026: 00.45.54 ❌
- Failed at implementation
- We want the next slightly bigger arrangement, not just any bigger one.
- We do this by modifying the sequence from the right side, because:
- The right side changes more frequently in lexicographic order.
- First decreasing element from right
Scan from right → left and find the first indexiwhere:nums[i] < nums[i + 1]i = 0 1 2 3 4 5
nums[i] = 1 2 3 6 5 4
i = 2 | 3 < 6 ✅
i = 2(value =3)- The right side is non-increasing.
- That means it's already the largest possible arrangement of that suffix.
- So we must modify something to the left.
- Find the "just larger" number to swap
- From the right side (
[6, 5, 4]), find the smallest number greater thannums[i] = 3. - We want the next smallest increase, not a big jump.
- For
3, candidates on right:4, Smallest >3 → 4
- From the right side (
- Swap
nums[i]with that numberNow we increased the number slightly.1 2 4 6 5 3 - Reverse the suffix
Reverse everything to the right of index
i = 21 2 4 | 6 5 3 → reverse → 1 2 4 | 3 5 6- The suffix was in descending order (largest)
- Reversing keeps it as the smallest possible arrangement
- Edge Case: Already Largest Permutation
If no breakpoint exists:
Entire array is descending → largest permutation
6 5 4 3 2 1
Solution: Reverse whole array1 2 3 4 5 6
- Failed at implementation
- 07 April, 2026: 00.45.54 ❌
- Array Leaders
- Longest Consecutive Sequences
- Set Zeros
- Rotate Matrix 90 Degree
- 07 April, 2026: 00.5.41 ❌
- Failed at optimization
- bottom left -> top right became result diagnal
- left lower value became upper right value
- Failed at optimization
- 07 April, 2026: 00.5.41 ❌
- Print Spiral Manner
- 07 April, 2026: 00.26.54 ❌
- Failed at implementation
- 4 iterator is 4 index
- Handle cases for matrices like 1xN or Nx1, you may duplicate elements.
- Failed at implementation
- 07 April, 2026: 00.26.54 ❌